<?php
class ControllerAbstractAbstract extends Controller{
	
	private $error = array();
	
	public function index(){
		if(!$this->customer->isLogged()){
			$this->session->data['redirect'] = $this->url->link('abstract/abstract','','SSL');
			$this->redirect($this->url->link('account/login','','SSL'));
		}
	
		$this->language->load('abstract/abstract');
		$this->load->model('abstract/abstract');
		$this->load->model('tool/image');
	
		$this->document->setTitle($this->language->get('heading_title'));
	
		$this->data['breadcrumbs'] = array();
	
		$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home'),
				'separator' => false
		);
	
		$this->data['breadcrumbs'][] = array(
				'text'      => '摘要提交',
				'href'      => $this->url->link('abstract/abstract', '', 'SSL'),
				'separator' => $this->language->get('text_separator')
		);
	
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['column_id'] = $this->language->get('column_id');
		$this->data['column_title'] = $this->language->get('column_title');
		$this->data['column_action'] = $this->language->get('column_action');
		$this->data['button_add_abstract'] = $this->language->get('button_add_abstract');
		$this->data['button_edit_abstract'] = $this->language->get('button_edit_abstract');
		$this->data['button_delete_abstract'] = $this->language->get('button_delete_abstract');
		$this->data['add'] = $this->url->link('abstract/abstract/add', '', 'SSL');
	
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
    		unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
	
		$customer_id = $this->customer->getId();
	
		$abstracts = $this->model_abstract_abstract->getAbstracts($customer_id);
		
		foreach ($abstracts as $abstract){
			$this->data['abstracts'][] = array(
					'abstract_id'	=> $abstract['abstract_id'],
					'title'			=> $abstract['title'],
					'content'		=> $abstract['content'],
					'session'		=> $abstract['session'],
					'edit'			=> $this->url->link('abstract/abstract/edit','abstract_id=' . $abstract['abstract_id'], 'SSL'),
					'delete'		=> $this->url->link('abstract/abstract/delete', 'abstract_id=' . $abstract['abstract_id'], 'SSL')
					);
		}
	
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/abstract/abstract_list.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/abstract/abstract_list.tpl';
		} else {
			$this->template = 'default/template/abstract/abstract_list.tpl';
		}
	
		$this->children = array(
				'common/header'
		);
	
		$this->response->setOutput($this->render());
	}
	
	public function add(){
		
		if(!$this->customer->isLogged()){
			$this->session->data['redirect'] = $this->url->link('abstract/abstract/add','','SSL');
			$this->redirect($this->url->link('account/login','','SSL'));
		}
		
		$this->language->load('abstract/abstract');
		
		$this->load->model('abstract/abstract');
		
		$this->document->setTitle($this->language->get('heading_title'));
		
		if(($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm() ){
			$this->model_abstract_abstract->addAbstract($this->request->post);
			$this->session->data['success'] = $this->language->get('text_add_success');
			$this->redirect($this->url->link('abstract/abstract'));
		}
		
		$this->getForm();
	}
	
	public function edit(){
		 if(!$this->customer->isLogged()){
		 	$this->session->data[redirect] = $this->url->link('abstract/abstract/edit','','SSL');
		 	$this->redirect($this->url->link('account/edit','','SSL'));
		 }
		 
		 $this->language->load('abstract/abstract');
		 $this->load->model('abstract/abstract');
		 
		 $this->document->setTitle($this->language->get('heading_title'));
		 
		 if(($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()){
		 	$this->model_abstract_abstract->editAbstract($this->request->get['abstract_id'], $this->request->post);
		 	$this->session->data['success'] = $this->language->get('text_edit_success');
		 	$this->redirect($this->url->link('abstract/abstract'));
		 }
		 $this->getForm();
	}
	
	public function delete(){
		if(!$this->customer->isLogged()){
			$this->session->data['redirect'] = $this->url->link('abstract/abstract/delete','','SSL');
			$this->redirect($this->url->link('abstract/abstract','','SSL'));
		}
		$this->language->load('abstract/abstract');
		$this->load->model('abstract/abstract');
		
		$this->document->setTitle($this->language->get('heading_title'));
		
		if(isset($this->request->get['abstract_id']) && $this->validateDelete()){
			$this->model_abstract_abstract->deleteAbstract($this->request->get['abstract_id']);
			$this->session->data['success'] = $this->language->get('text_delete_success');
			$this->redirect($this->url->link('abstract/abstract'));
		}
	}
	
	public function getForm(){
		
		$this->data['breadcrumbs'] = array();
		
		$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_home'),
				'href'      => $this->url->link('common/home'),
				'separator' => false
		);
		
		$this->data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_abstract'),
				'href'      => $this->url->link('abstract/abstract', '', 'SSL'),
				'separator' => $this->language->get('text_separator')
		);
		
		if(!isset($this->request->get['abstract_id']))
		{
			$this->data['breadcrumbs'][] = array(
					'text'      => '提交新摘要',
					'href'      => $this->url->link('abstract/abstract/add', '', 'SSL'),
					'separator' => $this->language->get('text_separator')
			);
		}else{
			$this->data['breadcrumbs'][] = array(
					'text'      => '修改摘要',
					'href'      => $this->url->link('abstract/abstract/edit', 'abstract_id=' . $this->request->get['abstract_id'], 'SSL'),
					'separator' => $this->language->get('text_separator')
			);
		}
		
		
		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_content'] = $this->language->get('entry_content');
		$this->data['entry_session'] = $this->language->get('entry_session');
		$this->data['text_session1'] = $this->language->get('text_session1');
		$this->data['text_session2'] = $this->language->get('text_session2');
		$this->data['text_session3'] = $this->language->get('text_session3');
		$this->data['button_continue'] = $this->language->get('button_continue');
		
		if(isset($this->error['title'])){
			$this->data['error_title'] = $this->language->get('error_title');
		}else{
			$this->data['error_title'] = '';
		}
		
		if(isset($this->error['content'])){
			$this->data['error_content'] = $this->language->get('error_content');
		}else{
			$this->data['error_content'] = '';
		}
		
		if(isset($this->error['session'])){
			$this->data['error_session'] = $this->language->get('error_session');
		}else{
			$this->data['error_session'] = '';
		}
		
		if(!isset($this->request->get['abstract_id'])){
			$this->data['action'] = $this->url->link('abstract/abstract/add','','SSL');
		}else{
			$this->data['action'] = $this->url->link('abstract/abstract/edit','abstract_id=' . $this->request->get['abstract_id'],'SSL');
		}
		
		if(isset($this->request->get['abstract_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')){
			$abstract_info = $this->model_abstract_abstract->getAbstract($this->request->get['abstract_id']);
		}
		
		if(isset($this->request->post['title'])){
			$this->data['title'] = $this->request->post['title'];
		}elseif(!empty($abstract_info)){
			$this->data['title'] = $abstract_info['title'];
		}else{
			$this->data['title'] = '';
		}
		
		if(isset($this->request->post['content'])){
			$this->data['content'] = $this->request->post['content'];
		}elseif(!empty($abstract_info)){
			$this->data['content'] = $abstract_info['content'];
		}else{
			$this->data['content'] = '';
		}

		if(isset($this->request->post['session'])){
			$this->data['session'] = $this->request->post['session'];
		}elseif(!empty($abstract_info)){
			$this->data['session'] = $abstract_info['session'];
		}else{
			$this->data['session'] = '';
		}
		
		if(file_exists(DIR_TEMPLATE . $this->config->get('config_template').'/template/abstract/abstract_form.tpl')){
			$this->template = $this->config->get('config_template') . '/template/abstract/abstract_form.tpl';
		}else{
			$this->template = 'default/template/abstract/abstract_form.tpl';
		}
		
		
		$this->children = array(
				'common/header'
		);
		
		$this->response->setOutput($this->render());
	}
	
	public function validateForm(){
		$title = $this->request->post['title'];
		if((utf8_strlen($title) < 1) || (utf8_strlen($title) > 255)){
			$this->error['title'] = $this->language->get('error_title');
		}
		
		$content = $this->request->post['content'];
		if(utf8_strlen($content) < 1){
			$this->error['content'] = $this->language->get('error_content');
		}
		
		if(!$this->error){
			return true;
		}else{
			return false;
		}
	}
	
	public function validateDelete(){
		return true;
	}
}